package MyApp.View
{
	import MyApp.Context;
	import MyApp.Model.LoginUser;
	import MyApp.Model.User;
	import MyApp.Model.proxy.LoginProxy;
	
	import components.login.LoginView;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.ChildExistenceChangedEvent;
	import mx.events.FlexEvent;
	import mx.logging.ILogger;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	import mx.utils.StringUtil;
	
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import util.Constants;
	import util.GDUtil;
	
	public class LoginMediator extends Mediator
	{
		public static const NAME:String="LoginMediator";
		public static const INIT_PORTAL_VIEW_OK:String = GDUtil.getUID();
		[Bindable]
		private var loginUser:User;
		public function LoginMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			loginView.addEventListener(LoginView.DO_LOGIN,loginHandel);
			loginView.addEventListener(FlexEvent.HIDE,hide);
		}
		private function get loginView():LoginView{
			return viewComponent as LoginView;
		}
		
		
		private function hide(e:Event):void {
			loginView.reset();
		}
		
		
		private function loingProxy():LoginProxy {
			facade.registerProxy(new LoginProxy(LoginProxy.NAME));
			return facade.retrieveProxy(LoginProxy.NAME) as LoginProxy;
		}
		
		
		private function get userRemote():RemoteObject {
			var remote:RemoteObject = GDUtil.remoteObject();
			remote.endpoint = Constants.REMOTE_IP;
			remote.destination = Constants.SER_USER;
			return remote;
		} 
		
		
		
		public function loginHandel(e:MouseEvent):void {
						
			var loginAo:AbstractOperation = userRemote.getOperation("getUser");
			loginAo.addEventListener(ResultEvent.RESULT,loginAoResult);
			loginAo.addEventListener(FaultEvent.FAULT,loginAoFault);
			
			var nickName:String = StringUtil.trim(loginView.userName.text);
			
			loginAo.send(nickName);
		}
		
		private function loginAoResult(e:ResultEvent):void {
			var user:User = e.result as User;
			loginView.mess = "";
			loginView.isVisibleMess = false;
			if(user) {
				loginUser = user;
				if(loginView.password.text == user.password) {
					try{
						doLogin();
						//将登录人记录在session中
						var loginAo:AbstractOperation = userRemote.getOperation("addLoginLog");
						loginAo.addEventListener(FaultEvent.FAULT,function (e:FaultEvent):void{});
						loginAo.send(user);
						//将登录人缓存本地
						var loginuser:LoginUser=new LoginUser();
						loginuser.name=user.realname;
						loginuser.username=user.username;
						loginuser.role=user.roleid;
						loginuser.user=user;
						Context.instance.setProperty(Constants.LOGINUSER_KEY,loginuser);;//放入context中
						Constants.USER_NAME = user.realname;
					}catch(e:Error){
						trace(e.message);
					}
				}else {
					loginView.mess = "对不起，您的密码不正确!";
					loginView.isVisibleMess = true;
				}
				
			}else {
				loginView.mess = "用户名不存在！";
				loginView.isVisibleMess = true;
			}
		}
		private function loginAoFault(e:FaultEvent):void {
			Alert.show(e.toString());
			loginView.mess = "登陆失败!";
			loginView.isVisibleMess = true;
		}
		
		
		
		private function doLogin():void{
			FlexGlobals.topLevelApplication.vs.selectedChild=
				FlexGlobals.topLevelApplication.main;
			if(!facade.hasMediator(MainDeskMediator.NAME)) {
				facade.registerMediator(new MainDeskMediator(MainDeskMediator.NAME,FlexGlobals.topLevelApplication.main));
			}
			//初始化桌面
			initDeskData();
			
		}
		private function initDeskData():void{
			sendNotification(INIT_PORTAL_VIEW_OK,[]);
			
//			var remote:RemoteObject =  GDUtil.remoteObject();
//			remote.destination = Constants.AUTHORITY_SER;
//			var aoAuthority:AbstractOperation = remote.getOperation("loadParentAuthoritiesByRole");;
//			aoAuthority.addEventListener(ResultEvent.RESULT,function(e:ResultEvent):void{
//				sendNotification(INIT_PORTAL_VIEW_OK,e.result);
//			});
//			aoAuthority.addEventListener(FaultEvent.FAULT,function (e:FaultEvent):void {
//				Alert.show("加载数据失败!");			
//			});
//			aoAuthority.send(loginUser.part);
			
		}
	}
}